<template>
  <div class="log-container">
    <el-page-header content="发布商品" @back="onCancel" />
    <el-divider />
    <el-form :model="form" label-width="120px" style="width:30vw">
      <el-form-item label="名称">
        <el-input v-model="form.name" />
      </el-form-item>
      <el-form-item label="型号">
        <el-input v-model="form.model" />
      </el-form-item>
      <el-form-item label="价格（元）">
        <el-input v-model="form.price" />
      </el-form-item>
      <el-form-item label="当时价格（元）">
        <el-input v-model="form.originPrice" />
      </el-form-item>
      <el-form-item label="折旧程度">
        <el-input v-model="form.oldLevel" />
      </el-form-item>
      <el-form-item label="购买时间">
        <el-input v-model="form.buyTime" placeholder="yyyy-MM-dd"/>
      </el-form-item>
      <el-form-item label="分类">
        <el-select v-model="form.type" placeholder="请选择">
          <el-option
            v-for="item in typeList"
            :key="item"
            :label="item"
            :value="item"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="简介">
        <el-input v-model="form.description" type="textarea" />
      </el-form-item>
      <el-upload
        action="http://localhost:8082/api/comm/upload"
        list-type="picture-card"
        :on-success="uploadSuccess"
        :on-preview="handlePictureCardPreview"
        :on-remove="handleRemove"
        drag
        multiple
      >
        <i class="el-icon-upload" />
      </el-upload>
      <el-form-item>
        <el-popconfirm title="确认提交吗？" @confirm="onSubmit(form)">
          <el-button slot="reference" type="primary">提交</el-button>
        </el-popconfirm>
        <el-button @click="onCancel">取消</el-button>
      </el-form-item>
    </el-form>
    <el-dialog :visible.sync="dialogVisible">
      <img width="100%" :src="dialogImageUrl" alt="">
    </el-dialog>
  </div>
</template>

<script>
import { successMsg } from '@/utils/comm'
import { addComm, urlGetComm } from '@/api/comm'

export default {
  data() {
    return {
      module: 'product',
      form: {},
      resourceList: [],
      dialogImageUrl: '',
      dialogVisible: false,
      typeList: []
    }
  },
  created() {
    this.getTypeList()
  },
  methods: {
    getTypeList() {
      this.listLoading = true
      urlGetComm('/product/list/type').then(response => {
        this.typeList = response.data
        this.listLoading = false
      })
    },
    onSubmit(form) {
      this.form.images = JSON.stringify(this.resourceList)
      addComm(this.module, form).then(response => {
        successMsg()
      })
    },
    onCancel() {
      this.$router.push('/')
    },
    uploadSuccess(response, uploadFile, uploadFiles) {
      console.log(response)
      this.resourceList.push(response.data)
      successMsg()
    },
    handleRemove(file, fileList) {
      console.log(file, fileList)
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url
      this.dialogVisible = true
    }
  }
}
</script>

<style scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}

</style>

